package com.pet.petadoption.pet.controller;

import com.pet.petadoption.pet.biz.PetBiz;
import com.pet.petadoption.pet.biz.petBizImpl.PetBizImpl;
import com.pet.petadoption.pet.entity.Pet;
import com.pet.petadoption.user.entity.User;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Controller
@RequestMapping("pet")
@Log4j2
@CrossOrigin(origins = "http://localhost:8080",maxAge = 36000)
public class PetController {
    @Autowired
    PetBizImpl petBiz;



    @RequestMapping(value = "/getAllPetNoAdopted")
    @ResponseBody
    public Map<String,Object> getAllPetNoAdopted(Integer index) {
        Integer newIndex = index;
        if (newIndex == null) {
            newIndex = 1;
        }
        int size = 5;
        List<Pet> list = petBiz.getAllPet(newIndex, size);
        System.out.println(list.toString());
        Map<String, Object> map = new HashMap<>();
        map.put("index", index);
        map.put("list", list);
        return map;
    }


    @RequestMapping(value = "/getAllPet")
    @ResponseBody
    public Map<String,Object> getAllPet(Integer index) {
        Integer newIndex = index;
        if (newIndex == null) {
            newIndex = 1;
        }
        int size = 5;

        List<Pet> list = petBiz.getAllPet(newIndex, size);
        System.out.println(list.toString());
        Map<String, Object> map = new HashMap<>();
        map.put("index", index);
        map.put("list", list);
        return map;
    }

    @RequestMapping(value = "/addPet")
    @ResponseBody
    public boolean addPet(String name, String type, String age,
                           String sex, String info, String img) {
    Pet pet  = new Pet();
    pet.setName(name);
    pet.setType(type);
    pet.setAge(Integer.valueOf(age));
    pet.setSex(sex);
    pet.setInfo(info);
    pet.setImg(img);
   return petBiz.addPet(pet);
    }

    @RequestMapping(value = "/modifyPet")
    @ResponseBody
    public boolean modifyPet(String name, String type, String age,
                          String sex, String info, String img,String is_adopted,String id) {
        Pet pet  = new Pet();
        pet.setName(name);
        pet.setType(type);
        pet.setAge(Integer.valueOf(age));
        pet.setSex(sex);
        pet.setInfo(info);
        pet.setImg(img);
        pet.setId(Integer.valueOf(id));
        if(!is_adopted.equals("false")){
            pet.setIs_adopted(true);
        }else{
            pet.setIs_adopted(false);
        }
        log.warn(pet);
        if(petBiz.modifyPet(pet)==1){
            return true;
        }else{
            return false;
        }

    }


    @RequestMapping(value = "/findPetById")
    @ResponseBody
    public List<Pet> findPetById(String id) {
        Pet pet =  petBiz.findPetById(Integer.valueOf(id));
        log.warn(pet +" "+ id);
        List<Pet> list = new ArrayList<Pet>();

        if(pet==null){
            return list;
        }
        list.add(pet);
        return list;
    }

    @RequestMapping(value = "/findPetByInfo")
    @ResponseBody
    public List<Pet> findPetByInfo(String info) {
        System.out.println(info);
        return petBiz.findPetByInfo(info);
    }


    @RequestMapping(value = "/delPet")
    @ResponseBody
    public Integer delPet(Integer id){

        return petBiz.delPet(id);
    }

    @RequestMapping(value = "/delArr")
    @ResponseBody
    public boolean delArr(String ids) {
        Integer row = petBiz.delArr(ids);
        log.warn(row);
        return row>0;
    }



}
